package Homework.cms.articles;

import com.xcms.test.news.News;
import com.xcms.test.news.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UpdateArticlesServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");     //告诉它当前文档的编码
        response.setHeader("Content-Type","text/html;utf-8");    //告诉浏览器文档类型
        String title=request.getParameter("title");
        String content=request.getParameter("content");
        String id=request.getParameter("id");
        String writter=request.getParameter("writter");

        PrintWriter out=response.getWriter();   //获取一个打印流，向网页输出打印

        //out.println(id+" "+writter+" "+title+" "+content);

        Articles articles=new Articles();
        articles.setTitle(title);
        articles.setContent(content);
        articles.setId(Integer.parseInt(id));
        articles.setWritter(writter);

        if(ArticlesDao.update(articles)){
            out.println("修改成功！");
            response.setHeader("Refresh","3;url=http://localhost:8080/webtest/Homework/manage/articles?action=userpage&currPage=1&writter="+articles.getWritter());
        }
        else {
            out.println("修改失败！");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
